import {Http} from '@angular/http';
import {NgLayer, NgLayerRef} from 'ng2-layer';
import {Component,ViewContainerRef, Injectable}  from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {FormsModule}  from '@angular/forms';
import {DirectivesM} from "app/Directives";
import {CommonModule} from "@angular/common";

/**
 * 左侧菜单部分
 */
@Component({
	selector: '.box_product',
	templateUrl: 'template/router/boxProduct/product.html'
})
export class BoxProductComp {
	addresses:any;
	boxId:any;
	products:any = [];
	product:any = {status:true};
	selectedImg:any={};
	constructor(private http:Http, router:ActivatedRoute, private layer:NgLayer, private parent:ViewContainerRef){
		//加载产品列表
		router.parent.params.subscribe(params => {
			this.boxId = params.id;
			this.loadProduct();
		});
	}
	
	//加载商品
	loadProduct(){
		this.http.get({
			search:{
				m:"Admin",
				c:"ApiGoods",
				a:"getGoodsList",
				box_id:this.boxId
			}
		}).then((data)=>{
			this.products = data.data.goods_data;
		});
	}
	
	//显示添加商品面板
	showAdd(){
		this.showAddProduct = true;
		this.product = {status:true};
		this.selectedImg={};
	}
	
	//编辑产品
	edit(p){
		
	}
	
	//上架或下架
	upOrDown(p){
		this.http.post({
			search:{
				m:"Admin",
				c:"ApiGoods",
				a:"goodsPutaway"
			},
			body:{
				goods_id:p.goods_id,
				status:p.status=="0"?"1":"0"
			}
		}).then(()=>{
			p.status=p.status=="0"?"1":"0";
		});
	}
	
	//删除商品
	deleteProduct(p){
		this.http.post({
			search:{
				m:"Admin",
				c:"ApiGoods",
				a:"delGoods"
			},
			body:{
				goods_id:p.goods_id
			}
		}).then(()=>{
			this.products = this.products.filter((g)=>{
				return g!=p;
			});
		});
	}
	
	//设置产品分类
	updateGoodCategory(p){
		
	}
	
	//保存商品
	addProduct(){
		this.http.post({
			search:{
				m:"Admin",
				c:"ApiGoods",
				a:"addGoods"
			},
			body:{
				name:this.product.name,
				price:this.product.price,
				price_cost:this.product.price_cost,//（非必填）  //成本价，没有默认为售价
				innage:this.product.innage,    //库存
				intro:this.product.content,//（非必填） //以后可能用到的富文本内容展示
				status:this.product.status?1:0, //(非必填)  //是否上架，默认为不上架，1未上架，0为不上架
				img_list:this.selectedImg.image_id, //(图片id)
				box_id:this.boxId,     //盒子id
				open_innage_tip:1,
				min_innage:1
			}
		}).then(()=>{
			this.showAddProduct =  false;
			this.loadProduct();
		});
	}
	
	//选择图片弹窗
	selectImages(){
		this.layer.dialog({
			title:"选择产品图标",
			parent:this.parent,
			dialogComponent:selectImageComp,
			imports:[FormsModule, DirectivesM, CommonModule],
			data:{parent:this}
		});
	}
}

//选择图片弹窗
@Component({templateUrl:"template/router/boxProduct/selectImages.html"})
class selectImageComp{
	images:any = [];
	parent:any;
	
	constructor(private http:Http, private ly:NgLayerRef){
		//获取用户上传的图片
		/*http.get({
			search:{
				m:"Admin",
				c:"ApiImage",
				a:"getImageListByAdminId"
			}
		}).then((data)=>{
			
		});*/
	}
	
	//弹窗打开的时候如果有产品名字,马上搜索图片
	ngOnInit(){
		if(this.parent.product.name){
			this.keyWord = this.parent.product.name;
			this.search();
		}
	}
	
	search(){
		//搜索图标库
		this.http.post({
			search:{
				m:"Admin",
				c:"ApiGoods",
				a:"getImageListByKey"
			},
			body:{
				keyword:this.keyWord,
				count:100,
				page:1
			}
		}).then((data)=>{
			this.images = data.data;
		});
	}
	
	select(img){
		this.selectedImg = img;
	}
	
	apply(){
		this.parent.selectedImg = this.selectedImg;
		this.ly.close();
	}
}
